package com.southminority.ethnic.until;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Component
public class AesUtil {
    private static  String AES_KEY = "1234567890123456";
    @Value("${encryption.aes.wallet-management-key:1234567890123456}")
    private void setWalletManagementKey(String walletManagementKey) {
        AES_KEY = walletManagementKey;
        log.info("walletManagementKey: {}", "OK");
    }



    /// /////////////////////////////
    private static final String AES_ALGORITHM = "AES";

    private static final String UTF8 = StandardCharsets.UTF_8.name();

    /* AES加密 String */
    public static String encryptStrAES(String text) throws IllegalBlockSizeException, BadPaddingException {
        byte[] encodeBytes = encryptByteAES(text.getBytes(StandardCharsets.UTF_8), AES_KEY);
        return Base64.encodeBase64String(encodeBytes);
    }

    /* AES解密 String*/
    public static String decryptStrAES(String text) throws IllegalBlockSizeException, BadPaddingException {

        byte[] decodeBytes = decryptByteAES(Base64.decodeBase64(text.getBytes(StandardCharsets.UTF_8)), AES_KEY);
        return new String(decodeBytes, StandardCharsets.UTF_8);
    }

    /* AES加密 originalBytes */
    public static byte[] encryptByteAES(byte[] originalBytes, String key) throws IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = getAESCipher(key, Cipher.ENCRYPT_MODE);
        byte[] encodeBytes = null;

        encodeBytes = cipher.doFinal(originalBytes);

        return encodeBytes;
    }

    /* AES解密 encryptedBytes */
    public static byte[] decryptByteAES(byte[] encryptedBytes, String key) throws IllegalBlockSizeException, BadPaddingException {

        Cipher cipher = getAESCipher(key, Cipher.DECRYPT_MODE);
        byte[] decodeBytes = null;

        decodeBytes = cipher.doFinal(encryptedBytes);

        return decodeBytes;
    }

    public static Cipher getAESCipher(String key, int mode){

        Cipher cipher = null;
        SecretKey secretKey;
        try {
            cipher = Cipher.getInstance(AES_ALGORITHM);
            byte[] keyBytes = key.getBytes(UTF8);
            secretKey = new SecretKeySpec(keyBytes, AES_ALGORITHM);
            cipher.init(mode, secretKey);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        return cipher;
    }

}
